/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package locusts.server.behaviours;

import javax.vecmath.Vector2d;
import locusts.common.entities.Actor;
import locusts.common.entities.Entity;

/**
 *
 * @author Hamish Morgan
 */
public class SeekEnergy extends AbstractBehaviour {


 protected static final double DEFAULT_MAX_DISTANCE = Double.POSITIVE_INFINITY;
    protected Entity target;
    protected  double maxDistance;

    public SeekEnergy(double weight, Entity target, double maxDistance) {
        super(weight);
        setTarget(target);
        this.maxDistance = maxDistance;
    }

    public SeekEnergy(double weight, Entity target) {
        this(weight, target, DEFAULT_MAX_DISTANCE);
    }
    public SeekEnergy(Entity target) {
        super();
        setTarget(target);
        this.maxDistance = DEFAULT_MAX_DISTANCE;
    }

    public void setTarget(Entity target) {
        if (target == null)
            throw new IllegalArgumentException();
        this.target = target;
    }

    public void clearTarget() {
        this.target = null;
    }

    public void setMaxDistance(double maxDistance) {
        this.maxDistance = maxDistance;
    }

    public double getMaxDistance() {
        return maxDistance;
    }

    public Vector2d getSteering(Actor boid) {
        Vector2d steering = new Vector2d();
        if (getWeight() == 0)
            return steering;
        if (target == null || target.getEnergy() <= 0)
            return steering;

        double dx = target.getX() - boid.getX();
        double dy = target.getY() - boid.getY();
        double d = (dx*dx + dy*dy);
//        if(d > maxDistance*maxDistance)
//            return steering;

        steering.set(dx*(100/d), dy*(100/d));
//        steering.set(dx*(1/d), dy*(1/d));
//
//        steering.scale((double) getWeight());
        steering.scale(1f / (boid.getEnergy()+1));
        steering.scale(target.getEnergy());

        return steering;
    }
    

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final SeekEnergy other = (SeekEnergy) obj;
        if (this.target != other.target &&
                (this.target == null || !this.target.equals(other.target)))
            return false;
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        int hash = super.hashCode();
        hash = 29 * hash + (this.target != null ? this.target.hashCode() : 0);
        return hash;
    }

}
